package com.neuedu.mp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neuedu.mp.entity.SysRole;
import com.neuedu.mp.service.ISysRoleService;
import com.neuedu.mp.vo.RoleMenuVO;
import com.neuedu.mp.vo.SysMenuVO;
import com.neuedu.mp.vo.UserRoleGrant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author 金山老师
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/sysRole")
@Slf4j
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    ISysRoleService sysRoleService;



    @ApiOperation(value = "角色保存")
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody SysRole role) {

        System.out.println(role);
        log.debug(role.toString());

        return sysRoleService.saveOrUpdate(role);

    }


    @ApiOperation(value = "角色更新")
    @PostMapping("/update")
    public boolean update(SysRole role) {

        log.debug(role.toString());

        return sysRoleService.updateById(role);

    }


    @ApiOperation(value = "根据主键查询角色")
    @GetMapping("/getById/{id}")
    public SysRole getById(@PathVariable Integer id) {
        return sysRoleService.getById(id);
    }


    @ApiOperation(value = "角色列表查询")
    @GetMapping("/list")
    @ApiImplicitParams(
            value={
                    @ApiImplicitParam(name="roleName" ,value="角色名称")
            }
    )
    public List<SysRole> list(String roleName) {


        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<SysRole>();
        if(roleName != null){
            queryWrapper.like(SysRole::getRoleName, roleName);
        }

        return sysRoleService.list(queryWrapper);

    }


    @ApiOperation(value = "角色列表分页查询")
    @GetMapping("/page")
    @ApiImplicitParams(
            value={
                    @ApiImplicitParam(name="roleName" ,value="角色名称"),
                    @ApiImplicitParam(name="current" ,value="分页-页号"),
                    @ApiImplicitParam(name="size" ,value="分页-每页条数")
            }
    )
    public Page<SysRole> page(@RequestParam(required = false,defaultValue = "1")  Integer current,
                              @RequestParam(required = false,defaultValue = "10") Integer size,
                              String roleName) {


        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<SysRole>();
        if(roleName != null){
            queryWrapper.like(SysRole::getRoleName, roleName);
        }

        Page<SysRole> page = new Page<SysRole>(current,size);
        sysRoleService.page(page,queryWrapper);

        return page;
    }


    @ApiOperation(value = "角色根据主键删除")
    @GetMapping("/removeById/{id}")
    public boolean removeById(@PathVariable long id) {
        return sysRoleService.removeById(id);

    }


    @ApiOperation(value = "给角色授权菜单")
    @PostMapping("/grant")
    public boolean grant(@RequestBody RoleMenuVO grant ) throws JsonProcessingException {

        return sysRoleService.grant(grant);
    }



    @ApiOperation(value = "查询已经授权的菜单权限")
    @GetMapping("/loadMenuIdsByRole/{roleId}")
    public List<Long> loadMenuIdsByRole(@PathVariable("roleId") Long roleId) {
        return sysRoleService.loadMenuIdsByRole(roleId);

    }
}
